import 'package:event_bus/event_bus.dart';

class GEventBusUtils {
  static final Map<String, EventBus> _eventBusMap = {};
  static final EventBus defaultBus = EventBus();
  //factory的定义是用来修饰构造函数时，并不总是创建一个新的该类的对象，比如它可能会从缓存中返回一个已有的实例，或者是返回子类的实例
  factory GEventBusUtils() => const GEventBusUtils._a();

  //使构造函数私有
  const GEventBusUtils._a();

  EventBus getDefault() {
    return defaultBus;
  }

  EventBus getBus(String key) {
    EventBus? bus = _eventBusMap[key];
    bus ??= EventBus();
    _eventBusMap[key] = bus;
    return bus;
  }
}
